<template>
  <el-form
    ref="formRef"
    :model="formData"
    :rules="formRules"
    v-loading="formLoading"
    label-width="0px"
    :inline-message="true"
  >
    <el-table :data="formData" class="-mt-10px">
      <el-table-column label="序号" type="index" width="100" />
       <el-table-column label="作业类别" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.type`" :rules="formRules.type" class="mb-0px!">
            <el-select v-model="row.type" placeholder="请选择作业类别">
                <el-option
                  v-for="dict in getIntDictOptions(DICT_TYPE.CRM_OPERAT_TYPE)"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
            </el-select>
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="姓名" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.name`" :rules="formRules.name" class="mb-0px!">
            <el-input v-model="row.name" placeholder="请输入姓名" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="资格证号码" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.license`" :rules="formRules.license" class="mb-0px!">
            <el-input v-model="row.license" placeholder="请输入资格证号码" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="资格证有限日期" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.effdate`" :rules="formRules.effdate" class="mb-0px!">
            <el-date-picker
              v-model="row.effdate"
              type="date"
              value-format="x"
              placeholder="选择资格证有限日期"
            />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="备注" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.note`" class="mb-0px!">
            <el-input v-model="row.note" placeholder="请输入备注" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column align="center" fixed="right" label="操作" width="60">
        <template #default="{ $index }">
          <el-button @click="handleDelete($index)" link>—</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-form>
  <el-row justify="center" class="mt-3">
    <el-button type="primary" plain @click="handleAdd" round
      ><Icon icon="ep:plus" class="mr-2" />增加行</el-button
    >
  </el-row>
</template>
<script setup lang="ts">
import { getIntDictOptions, DICT_TYPE } from '@/utils/dict'
import { ComplianceApi } from '@/api/crm/compliance'

const props = defineProps<{
  complianceId: undefined // 合规性编号（主表的关联字段）
}>()
const formLoading = ref(false) // 表单的加载中
const formData = ref([])
const formRules = reactive({
  complianceId: [{ required: true, message: '合规性编号不能为空', trigger: 'blur' }],
  type: [{ required: true, message: '作业类别不能为空', trigger: 'change' }],
  name: [{ required: true, message: '姓名不能为空', trigger: 'blur' }],
  license: [{ required: true, message: '资格证号码不能为空', trigger: 'blur' }],
  effdate: [{ required: true, message: '资格证有限日期不能为空', trigger: 'blur' }],
})
const formRef = ref() // 表单 Ref

/** 监听主表的关联字段的变化，加载对应的子表数据 */
watch(
  () => props.complianceId,
  async (val) => {
    // 1. 重置表单
    formData.value = []
    // 2. val 非空，则加载数据
    if (!val) {
      return;
    }
    try {
      formLoading.value = true
      formData.value = await ComplianceApi.getComplianceOperatorListByComplianceId(val)
    } finally {
      formLoading.value = false
    }
  },
  { immediate: true }
)

/** 新增按钮操作 */
const handleAdd = () => {
  const row = {
    id: undefined,
    complianceId: undefined,
    type: undefined,
    name: undefined,
    license: undefined,
    effdate: undefined,
    note: undefined,
  }
  row.complianceId = props.complianceId
  formData.value.push(row)
}

/** 删除按钮操作 */
const handleDelete = (index) => {
  formData.value.splice(index, 1)
}

/** 表单校验 */
const validate = () => {
  return formRef.value.validate()
}

/** 表单值 */
const getData = () => {
  return formData.value
}

defineExpose({ validate, getData })
</script>